package com.kangfu.api.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.kangfu.db.domain.entity.*;
import com.kangfu.db.mapper.*;
import com.kangfu.api.service.ILessonService;
import com.kangfu.db.model.vo.LessonInfoVo;
import com.kangfu.db.model.vo.UserPrescriptionLessonInfoVo;
import com.kangfu.db.model.vo.UserPrescriptionLessonVo;
import com.kangfu.db.model.vo.UserPrescriptionLessonWeekVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程视频Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-14
 */
@Service
public class LessonServiceImpl implements ILessonService
{
    @Autowired
    private LessonMapper lessonMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CheckinRecordMapper checkinRecordMapper;

    @Autowired
    private LessonPrescriptionMapper lessonPrescriptionMapper;


    @Autowired
    public UserPrescriptionItemMapper userPrescriptionItemMapper;

    @Autowired
    public UserPrescriptionMapper userPrescriptionMapper;

    @Autowired
    public LessonPointPrescriptionMapper lessonPointPrescriptionMapper;

    /**
     * 获取某一天的推荐课程
     * @param userId
     * @param localDate
     * @return
     */
    @Override
    public List<UserPrescriptionLessonVo> selectUserPrescriptionLessonByDate(int userId, LocalDate localDate) {
        List<UserPrescriptionLessonVo> userPrescriptionLessonVos = new ArrayList<>();
        UserPrescription userPrescription = userPrescriptionMapper.getUserPrescriptionByDate(userId, localDate);
        //获取课程信息
        if (userPrescription != null) {
            Map<Integer, UserPrescriptionItem> userPrescriptionItemsMap = userPrescriptionItemMapper.selectListByPrescriptionId(userPrescription.getId());

            if (1 == userPrescription.getType()) {
                //标准处方
                LessonPrescription lessonPrescription = lessonPrescriptionMapper.selectByWeek(userPrescription.getWeekType());

                List<LessonPrescriptionItem> lessonPrescriptionItems = lessonPrescriptionMapper.selectItemsByPrescriptionId(lessonPrescription.getId());
                for (LessonPrescriptionItem lessonPrescriptionItem : lessonPrescriptionItems) {
                    UserPrescriptionLessonVo userPrescriptionLessonVo = new UserPrescriptionLessonVo();
                    userPrescriptionLessonVo.setTitle(lessonPrescriptionItem.getTitle());
                    userPrescriptionLessonVo.setCover(lessonPrescriptionItem.getCover());
                    userPrescriptionLessonVo.setLessonTime(lessonPrescriptionItem.getItemTime());
                    userPrescriptionLessonVo.setRecommendId(userPrescriptionItemsMap.containsKey(lessonPrescriptionItem.getId()) ? userPrescriptionItemsMap.get(lessonPrescriptionItem.getId()).getId() : null);
                    userPrescriptionLessonVo.setToHistory(false);
                    userPrescriptionLessonVos.add(userPrescriptionLessonVo);
                }
            } else if (2 == userPrescription.getType()) {
                //指定分数运动处方
                LessonPointPrescription lessonPointPrescription = lessonPointPrescriptionMapper.selectById(userPrescription.getPointId());

                List<LessonPointPrescriptionItem> lessonPointPrescriptionItems = lessonPointPrescriptionMapper.selectItemsByPrescriptionId(lessonPointPrescription.getId());
                for (LessonPointPrescriptionItem lessonPointPrescriptionItem : lessonPointPrescriptionItems) {
                    UserPrescriptionLessonVo userPrescriptionLessonVo = new UserPrescriptionLessonVo();
                    userPrescriptionLessonVo.setTitle(lessonPointPrescriptionItem.getTitle());
                    userPrescriptionLessonVo.setCover(lessonPointPrescriptionItem.getCover());
                    userPrescriptionLessonVo.setLessonTime(lessonPointPrescriptionItem.getItemTime());
                    userPrescriptionLessonVo.setRecommendId(userPrescriptionItemsMap.containsKey(lessonPointPrescriptionItem.getId()) ? userPrescriptionItemsMap.get(lessonPointPrescriptionItem.getId()).getId() : null);
                    userPrescriptionLessonVo.setToHistory(false);
                    userPrescriptionLessonVos.add(userPrescriptionLessonVo);
                }
            }
        } else {
            //检查是否有往期课程
            UserPrescription historyUserPrescription = userPrescriptionMapper.getUserPrescriptionByUserId(userId);

            //如果没有推荐，那就给默认的课程
            LessonPrescription lessonPrescription = lessonPrescriptionMapper.selectByWeek(1);
            List<LessonPrescriptionItem> lessonPrescriptionItems = lessonPrescriptionMapper.selectItemsByPrescriptionId(lessonPrescription.getId());
            for (LessonPrescriptionItem lessonPrescriptionItem : lessonPrescriptionItems) {
                UserPrescriptionLessonVo userPrescriptionLessonVo = new UserPrescriptionLessonVo();
                userPrescriptionLessonVo.setTitle(lessonPrescriptionItem.getTitle());
                userPrescriptionLessonVo.setCover(lessonPrescriptionItem.getCover());
                userPrescriptionLessonVo.setLessonTime(lessonPrescriptionItem.getItemTime());
                userPrescriptionLessonVo.setRecommendId(null);
                if (historyUserPrescription == null) {
                    userPrescriptionLessonVo.setToHistory(false);
                } else {
                    userPrescriptionLessonVo.setToHistory(true);
                }
                userPrescriptionLessonVos.add(userPrescriptionLessonVo);
            }
        }
        return userPrescriptionLessonVos;
    }

    /**
     * 获取某个用户的推荐课程历史记录
     * @param userId
     * @param page
     * @return
     */
    @Override
    public List<UserPrescriptionLessonWeekVo> getLessonHistory(int userId, int page) {
        if (page < 1) {
            page = 1;
        }
        Integer pageSize = 5;
        Integer offset = (page - 1) * pageSize;
        List<UserPrescription> list = userPrescriptionMapper.getWeekUserPrescriptionList(userId, offset, pageSize);

        List<UserPrescriptionLessonWeekVo> userPrescriptionLessonWeekVos = new ArrayList<>();

        for (UserPrescription userPrescription : list) {
            UserPrescriptionLessonWeekVo userPrescriptionLessonWeekVo = new UserPrescriptionLessonWeekVo();

            userPrescriptionLessonWeekVo.setWeekOrder(userPrescription.getWeek());
            userPrescriptionLessonWeekVo.setRecommendTime(userPrescription.getTrainTime());
            List<UserPrescriptionLessonInfoVo> userPrescriptionLessonInfoVos = new ArrayList<>();

            Map<Integer, UserPrescriptionItem> userPrescriptionItemsMap = userPrescriptionItemMapper.selectListByPrescriptionId(userPrescription.getId());

            if (1 == userPrescription.getType()) {
                //标准处方
                LessonPrescription lessonPrescription = lessonPrescriptionMapper.selectByWeek(userPrescription.getWeekType());

                List<LessonPrescriptionItem> lessonPrescriptionItems = lessonPrescriptionMapper.selectItemsByPrescriptionId(lessonPrescription.getId());
                for (LessonPrescriptionItem lessonPrescriptionItem : lessonPrescriptionItems) {
                    UserPrescriptionLessonInfoVo userPrescriptionLessonInfoVo = new UserPrescriptionLessonInfoVo();
                    userPrescriptionLessonInfoVo.setTitle(lessonPrescriptionItem.getTitle());
                    userPrescriptionLessonInfoVo.setCover(lessonPrescriptionItem.getCover());
                    userPrescriptionLessonInfoVo.setLessonTime(lessonPrescriptionItem.getItemTime());
                    userPrescriptionLessonInfoVo.setRecommendId(userPrescriptionItemsMap.containsKey(lessonPrescriptionItem.getId()) ? userPrescriptionItemsMap.get(lessonPrescriptionItem.getId()).getId() : null);
                    userPrescriptionLessonInfoVo.setCurrentUserCheckinCount(userPrescriptionItemsMap.containsKey(lessonPrescriptionItem.getId()) ? userPrescriptionItemsMap.get(lessonPrescriptionItem.getId()).getCheckinCount() : 0);
                    userPrescriptionLessonInfoVos.add(userPrescriptionLessonInfoVo);
                }
            } else if (2 == userPrescription.getType()) {
                //指定分数运动处方
                LessonPointPrescription lessonPointPrescription = lessonPointPrescriptionMapper.selectById(userPrescription.getPointId());

                List<LessonPointPrescriptionItem> lessonPointPrescriptionItems = lessonPointPrescriptionMapper.selectItemsByPrescriptionId(lessonPointPrescription.getId());
                for (LessonPointPrescriptionItem lessonPointPrescriptionItem : lessonPointPrescriptionItems) {
                    UserPrescriptionLessonInfoVo userPrescriptionLessonInfoVo = new UserPrescriptionLessonInfoVo();
                    userPrescriptionLessonInfoVo.setTitle(lessonPointPrescriptionItem.getTitle());
                    userPrescriptionLessonInfoVo.setCover(lessonPointPrescriptionItem.getCover());
                    userPrescriptionLessonInfoVo.setLessonTime(lessonPointPrescriptionItem.getItemTime());
                    userPrescriptionLessonInfoVo.setRecommendId(userPrescriptionItemsMap.containsKey(lessonPointPrescriptionItem.getId()) ? userPrescriptionItemsMap.get(lessonPointPrescriptionItem.getId()).getId() : null);
                    userPrescriptionLessonInfoVo.setCurrentUserCheckinCount(userPrescriptionItemsMap.containsKey(lessonPointPrescriptionItem.getId()) ? userPrescriptionItemsMap.get(lessonPointPrescriptionItem.getId()).getCheckinCount() : 0);
                    userPrescriptionLessonInfoVos.add(userPrescriptionLessonInfoVo);
                }
            }

            userPrescriptionLessonWeekVo.setRecommendLessonList(userPrescriptionLessonInfoVos);
            userPrescriptionLessonWeekVos.add(userPrescriptionLessonWeekVo);
        }

        return userPrescriptionLessonWeekVos;

    }


    /**
     * 获取某个推荐课程的详情
     * @param recommendId
     * @return
     */
    @Override
    public LessonInfoVo getLessonInfo(int userId, int recommendId) {
        LessonInfoVo lessonInfoVo = new LessonInfoVo();
        lessonInfoVo.setRecommendId(recommendId);

        UserPrescriptionItem userPrescriptionItem = userPrescriptionItemMapper.getInfoByItemId(recommendId);
        UserPrescription userPrescription = userPrescriptionMapper.getUserPrescriptionById(userPrescriptionItem.getPrescriptionId());
        lessonInfoVo.setCurrentUserCheckinCount(userPrescriptionItem.getCheckinCount());
        if (1 == userPrescription.getType()) {
            //标准处方
            LessonPrescription lessonPrescription = lessonPrescriptionMapper.selectByWeek(userPrescription.getWeekType());

            LessonPrescriptionItem lessonPrescriptionItem = lessonPrescriptionMapper.selectItemById(userPrescriptionItem.getItemId());

            List<LessonPrescriptionItemLesson> lessonPrescriptionItemLessons = lessonPrescriptionMapper.selectItemLessonsByItemId(lessonPrescriptionItem.getId());

            lessonInfoVo.setTitle(lessonPrescriptionItem.getTitle());
            lessonInfoVo.setCover(lessonPrescriptionItem.getCover());
            lessonInfoVo.setLessonTime(lessonPrescriptionItem.getItemTime());
            lessonInfoVo.setCheckinCount(lessonPrescriptionItem.getCheckinCount());
            lessonInfoVo.setDesc(lessonPrescription.getDesc());
            lessonInfoVo.setAttention(lessonPrescription.getAttention());
            lessonInfoVo.setProhibition(lessonPrescription.getProhibition());
            lessonInfoVo.setPrice(lessonPrescription.getPrice());

            List<LessonInfoVo.VideoBean> videoBeans = new ArrayList<>();
            for (LessonPrescriptionItemLesson lessonPrescriptionItemLesson : lessonPrescriptionItemLessons) {
                LessonInfoVo.VideoBean videoBean = new LessonInfoVo.VideoBean();
                videoBean.setTitle(lessonPrescriptionItemLesson.getTitle());
                List<Lesson> lessons = lessonMapper.selectLessonList(lessonPrescriptionItemLesson.getLessonId());
                List<LessonInfoVo.VideoBean.UrlListBean> urlListBeans = new ArrayList<>();
                for (Lesson lesson : lessons) {
                    LessonInfoVo.VideoBean.UrlListBean urlListBean = new LessonInfoVo.VideoBean.UrlListBean();
                    urlListBean.setTitle(lesson.getTitle());
                    urlListBean.setUrl(lesson.getUrl());
                    urlListBeans.add(urlListBean);
                }
                videoBean.setList(urlListBeans);
                videoBeans.add(videoBean);
            }
            lessonInfoVo.setVideo(videoBeans);

        } else if (2 == userPrescription.getType()) {
            //指定分数运动处方
            LessonPointPrescription lessonPointPrescription = lessonPointPrescriptionMapper.selectById(userPrescription.getPointId());

            LessonPointPrescriptionItem lessonPointPrescriptionItem = lessonPointPrescriptionMapper.selectItemById(userPrescriptionItem.getItemId());

            List<LessonPointPrescriptionItemLesson> lessonPointPrescriptionItemLessons = lessonPointPrescriptionMapper.selectItemLessonsByItemId(lessonPointPrescriptionItem.getId());

            lessonInfoVo.setTitle(lessonPointPrescriptionItem.getTitle());
            lessonInfoVo.setCover(lessonPointPrescriptionItem.getCover());
            lessonInfoVo.setLessonTime(lessonPointPrescriptionItem.getItemTime());
            lessonInfoVo.setCheckinCount(lessonPointPrescriptionItem.getCheckinCount());
            lessonInfoVo.setDesc(lessonPointPrescription.getDesc());
            lessonInfoVo.setAttention(lessonPointPrescription.getAttention());
            lessonInfoVo.setProhibition(lessonPointPrescription.getProhibition());
            lessonInfoVo.setPrice(lessonPointPrescription.getPrice());

            List<LessonInfoVo.VideoBean> videoBeans = new ArrayList<>();
            for (LessonPointPrescriptionItemLesson lessonPointPrescriptionItemLesson : lessonPointPrescriptionItemLessons) {
                LessonInfoVo.VideoBean videoBean = new LessonInfoVo.VideoBean();
                videoBean.setTitle(lessonPointPrescriptionItemLesson.getTitle());
                List<Lesson> lessons = lessonMapper.selectLessonList(lessonPointPrescriptionItemLesson.getLessonId());
                List<LessonInfoVo.VideoBean.UrlListBean> urlListBeans = new ArrayList<>();
                for (Lesson lesson : lessons) {
                    LessonInfoVo.VideoBean.UrlListBean urlListBean = new LessonInfoVo.VideoBean.UrlListBean();
                    urlListBean.setTitle(lesson.getTitle());
                    urlListBean.setUrl(lesson.getUrl());
                    urlListBeans.add(urlListBean);
                }
                videoBean.setList(urlListBeans);
                videoBeans.add(videoBean);
            }
            lessonInfoVo.setVideo(videoBeans);

        } else {
            return null;
        }
        return lessonInfoVo;
    }

    /**
     * 打卡
     * @param userId
     * @param recommendId
     */
    @Override
    @Transactional
    public void checkin(Integer userId, Integer recommendId) {
        //添加当前用户运动处方课程打卡次数
        userPrescriptionItemMapper.checkin(recommendId);
        //添加原运动处方课程累计打卡次数
        UserPrescriptionItem userPrescriptionItem = userPrescriptionItemMapper.getInfoByItemId(recommendId);
        UserPrescription userPrescription = userPrescriptionMapper.getUserPrescriptionById(userPrescriptionItem.getPrescriptionId());
        if (userPrescription.getType() == 1) {
            //标准处方
            lessonPrescriptionMapper.checkin(userPrescriptionItem.getItemId());
        } else if  (userPrescription.getType() == 2) {
            //指定分数处方
            lessonPointPrescriptionMapper.checkin(userPrescriptionItem.getItemId());
        }
        //添加打卡记录
        CheckinRecord checkinRecord = new CheckinRecord();
        checkinRecord.setUserId(userId);
        checkinRecord.setPrescriptionId(userPrescriptionItem.getPrescriptionId());
        checkinRecord.setItemId(recommendId);
        checkinRecord.setCheckinDate(LocalDateTime.now());
        checkinRecord.setCreateTime(LocalDateTime.now());
        checkinRecordMapper.insertCheckinRecord(checkinRecord);

        //检查是不是今天第一次打卡，如果是的话，用户累计打卡次数加1，坚持打卡次数加1
        Integer todayCheckinCount = checkinRecordMapper.getCheckinRecordCountByUserId(userId, LocalDateTime.now());
        if (1 == todayCheckinCount) {
            userMapper.updateCheckinCount(userId);
            userMapper.updateGoonCount(userId);
        }
    }

}
